// MediaPortal status for LCDSmartie
// Based on DemoC++Plugin.cpp : Defines the entry points for the DLL application.
// also based on work done in mce_dll.
// Dave Perrow 2012
//
#define CPP
#include "MP.h"

#define DLLEXPORT __declspec(dllexport)

static int initialised=0;              // used to determine if SmartieInit is called more than once
static char result[MAX_CSTATE];        // Result string for functions to use (needs to be global


/*********************************************************
 *         SmartieInit                                   *
 *********************************************************/
extern "C" DLLEXPORT  void __stdcall  SmartieInit()
{
    // This function will be called when the plugin is 1st loaded.
	// This function is OPTIONAL.
	if (initialised==0)          // only do this stuff on first call to SmartieInit
      {  initialised=1;            // for some reason it sometimes gets called twice
         MP_init_globals();        // Initialise global values
         MP_kernel32_Start();      // Map the process query into the kernel32 library
         MP_open_log();            // Open the log file
         MP_read_cfg_file(1);      // read the configuration file
         MP_TCP_Start();           // Start the comms thread
         MP_check_timers(1);       // Do a dump and set up a timer for future
         MP_logl(3,"SmartieInit","Complete");
         MP_logl(3,"================================================================",
              "================================================================");
      }
    else MP_logl(2,"SmartieInit","Subsequent call ignored");
}

/*********************************************************
 *         SmartieFini                                   *
 *********************************************************/
extern "C" DLLEXPORT  void __stdcall  SmartieFini()
{ static int done=0;
    // Make sure this is only called once
    if (done==1) return;
    done=1;
    program_finished=1;
	// This function will be called just before the plugin
	// is unloaded. This function is OPTIONAL.
	MP_logl(3,"SmartieFini","Shutting Down");
	MP_Dump();                   // dump the contents of the tag structure
    MP_close_log();                // and close the log file
}

/*********************************************************
 *         GetMinRefreshInterval                         *
 *********************************************************/
extern "C" DLLEXPORT  int __stdcall  GetMinRefreshInterval()
{
	//
	// Define the minimum interval that a screen should get fresh
	// data from our plugin.
	// The actual value used by Smartie will be the higher of this
	// value and of the "dll check interval" setting
	// on the Misc tab.  [This function is optional, Smartie will
	// assume 300ms if it is not provided.]
	// 
	return 50; // 300 ms
}





/*********************************************************
 *         Function 1                                    *
 *  Returns the first display line                       *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function1(char *param1, char *param2)
{   MP_check_timers(0); return MP_display_first();  }


/*********************************************************
 *         Function 2                                    *
 *  Returns the second display line                      *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function2(char *param1, char *param2)
{   MP_check_timers(0); return MP_display_second();  }


/*********************************************************
 *         Function 3                                    *
 *  Returns the version number of this code              *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function3(char *param1, char *param2)
{   strcpy(result,"MediaPortal LCDSmartie plugin v"); strcat(result,VER_STRING);
    return result;
}

    
/************************************************************
 *         Function 4                                       *
 *  Returns the value of the tag(s) supplied as parameter(s)*
 *  If 2 tags are supplied then you will get string-string  *
 ***********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function4(char *param1, char *param2)
{ char retval[MAX_CSTATE+1];
  int data_type,istate;

  MP_check_timers(0); 

  if (strlen(param1)>0)
    { if ( MP_get_tag(param1, &data_type, &istate, result)==0) return "Unfound Param1 Tag"; 
      if (strlen(param2)>0)
        { if ( MP_get_tag(param2, &data_type, &istate, retval)==0) return "Unfound Param2 tag"; 
          else { strcat(result,"-"); strcat(result,retval); return result; }
        }
      else return result;
    }
  else if (strlen(param2)>0)
    { if ( MP_get_tag(param2, &data_type, &istate, result)==0) return "Unfound Param2 Tag";
      else return result;
    }
  else return "Param1,2 both empty";
}


/**********************************************************
 *         Function 5                                     *
 *  if param1 length > param2 then result=param1+"    "   *
 * designed to fix scrolling where there's no space       *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function5(char *param1, char *param2)
{ int i;

  sscanf(param2,"%d",&i); if (i<1) return "Invalid width for Param2";
  strcpy(result,param1);
  if (strlen(param1)>=i) strcat(result,"    ");
  return result;
}

/*********************************************************
 *         Function 6                                    *
 *  Performs an if function e.g. $dll(MP,6,a#b,c#d)      *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function6(char *param1, char *param2)
{ unsigned char str1[80],str2[80];
  static unsigned char str3[80],str4[80];
  int i,j;

  i=substrcpy(str1,(unsigned char *)param1,'#');       if(i==0) return "Param1 Missing";
  j=substrcpy(str2,(unsigned char *)&param1[i+1],'#'); if(j==0) return "Param2 Missing";
  i=substrcpy(str3,(unsigned char *)param2,'#');       if(i==0) return "Param3 Missing";
  j=substrcpy(str4,(unsigned char *)&param2[i+1],'#'); if(j==0) return "Param4 Missing"; 
  
  if(!strcmp((char *)str1,(char *)str2)) return (char *)str3;
  return (char *)str4;
}


/*********************************************************
 *         Function 7                                    *
 *  Returns the first display line (overriden if record) *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function7(char *param1, char *param2)
{   MP_check_timers(0); return MP_display_first_rec();  }

/*********************************************************
 *         Function 8                                    *
 *  Returns the second display line (overriden if record)*
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function8(char *param1, char *param2)
{   MP_check_timers(0); return MP_display_second_rec();  }


/*********************************************************
 *         Function 9                                    *
 *  Scroll a bit of the line use different ID's for      *
 *  different scrolls  (0-9)                             *
 *  param 1 = id#centre#width, param2=text               *
 *  if param2 size<width and centre=1 then centre text   *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function9(char *param1, char *param2)
{   int i,j, width, p2_len, id, centre;
    static char result[10][256];        // Result string for funtions to use (needs to be global
    static char last_result[10][256];   // Result string for functions to use (needs to be global
    static int position[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    char str[512];
    static int counter[10], init=0;

    if (init==0)
      { init=1; for (i=0;i<10;i++) { counter[i]=0; result[i][0]=0; last_result[i][0]=0; } }

    // check param values are valid
    id=atoi(param1);         if ((id<0) || (id>9)) return "Param1 ID invalid";
    centre=atoi(&param1[2]); if ((centre<0) || (centre>1)) return "Param1 Centre invalid";
    width=atoi(&param1[4]);  if (width<0) return "Param1 Width invalid";
    if (width>100) width=100;
    
    if ((++counter[id]) < MP_Global_scroll_count) return last_result[id];
    else { counter[id]=0; }

    // copy param2 into str till it's filled
    p2_len=strlen(param2); j=(512/p2_len)-1;
    strcpy(str,param2);
    if (width<p2_len)
      { for (i=0; i<j; i++) strcat(str,param2);
        // copy from the position for width characters   
        if (position[id]>=p2_len) position[id]=0;
        strncpy(result[id],&str[position[id]],width);
        position[id]=position[id]+1;
      }
    else if (centre==1)
      { i=(width-p2_len);
        if (i==1) i=0;
        else { i=i/2; // nr. spaces reqd.
               for (j=0; j<i; j++) result[id][j]=' ';
               strncpy(&result[id][i],str,p2_len);
               width=p2_len+i;
             }  
      }
    else { strncpy(result[id],str,width); }

    result[id][width]=0;
    strcpy(last_result[id],result[id]); last_result[id][width]=0;
    return result[id];
}


/*********************************************************
 *         Function 19                                   *
 *  Converts seconds to a time string as h:mm:ss         *
 *  if param2 provided then h:mm:ss/h:mm:ss              *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function19(char *param1, char *param2)
{  static char retval[30],retval2[10];

   MP_one_time_fmt(retval,"%d:%02d:%02d",atoi(param1));
   if (strlen(param2)>0) 
     { MP_one_time_fmt(retval2,"%d:%02d:%02d",atoi(param2));
       strcat(retval,"/"); strcat(retval,retval2);
     }
   return retval;
}


/**********************************************************
 *         Function 20                                    *
 *  Check if the given process is active (x.exe)          *
 * Returns param2 if it is, The value of the tag          *
 * ProcessInactive if not                                 *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function20(char *param1, char *param2)
{ if (MP_check_process(param1)==1) return param2; else return MP_Global_ProcessInactive; }

